# -*- coding: utf-8 -*-

import json
import datetime
import decimal
from bson import ObjectId

'''
    把数据库查询到的数据转换为json格式的工具类
'''


class MyJsonEncoder(json.JSONEncoder):
    '''
        重写构造json类，对日期等格式进行特殊处理
    '''

    def default(self, obj):
        if isinstance(obj, datetime.datetime):
            return obj.strftime('%Y-%m-%d %H:%M:%S')
        elif isinstance(obj, datetime.date):
            return obj.strftime('%Y-%m-%d')
        elif isinstance(obj, decimal.Decimal):
            return str(obj)
        elif isinstance(obj, ObjectId):
            return str(obj)
        else:
            return json.JSONEncoder.default(self, obj)


class Table2JsonYM(object):
    '''
        工具类，把MySQL数据库获取的数据转换为JSON格式
    '''


    @staticmethod
    def view_url_manage_listto_json(url_manage_list):
        data = []

        for row in url_manage_list:
            result = {}
            result['id'] = row[0]
            result['levelCode'] = row[1]
            result['deptName'] = row[2]
            result['deptUrl'] = row[3]
            result['desUrl'] = row[4]
            result['evidenceUrl'] = row[5]
            result['faceKeywords'] = row[6]
            result['oppositeKeywords'] = row[7]
            result['checkStatus'] = row[8]
            result['balanceStatus'] = row[9]

            result['checkTime'] = row[10]

            result['provinceId'] = row[11]
            result['cityId'] = row[12]
            result['districtId'] = row[13]

            result['enterPerson'] = row[14]
            result['enterTime'] = row[15]

            result['checkRemark'] = row[16]
            result['checkPerson'] = row[17]
            result['deptStatus'] = row[18]



            result['provinceName'] = row[19]
            result['cityName'] = row[20]
            result['districtName'] = row[21]
            result['LeveLId'] = row[22]
            result['LeveLName'] = row[23]


            data.append(result)
        jsonData = json.dumps(data, cls=MyJsonEncoder, ensure_ascii=False)

        return jsonData

    @staticmethod
    def view_ViewItem_and_reportCar_listto_json(url_manage_list):
        data = []

        for row in url_manage_list:
            result = {}
            result['itemId'] = row[0]
            result['itemHandlername'] = row[1]
            result['itemHandlerphone'] = row[2]
            result['itemTitle'] = row[3]
            result['itemPricerange'] = row[4]
            result['itemContent'] = row[5]
            result['itemUrl'] = row[6]
            result['itemDeadline'] = row[7]
            result['itemPublishdate'] = row[8]
            result['itemAddress'] = row[9]
            result['itemStatus'] = row[10]
            result['serviceId'] = row[11]
            result['serviceName'] = row[12]

            result['serviceCode'] = row[13]
            result['servicePrice'] = row[14]
            result['serviceRate'] = row[15]
            result['serviceStatus'] = row[16]
            result['serviceContent'] = row[17]
            result['serviceContract'] = row[18]
            result['serviceStartdate'] = row[19]

            result['serviceEnddate'] = row[20]
            result['deptId'] = row[21]
            result['deptName'] = row[22]
            result['deptPcode'] = row[23]
            result['deptCode'] = row[24]
            result['deptIsLock'] = row[25]
            result['deptSort'] = row[26]
            result['deptAddress'] = row[27]
            result['leveCode'] = row[28]
            result['provinceId'] = row[29]
            result['cityId'] = row[30]
            result['districtId'] = row[31]
            result['carId'] = row[32]
            result['userId'] = row[33]
            result['carStatus'] = row[34]


            data.append(result)
        jsonData = json.dumps(data, cls=MyJsonEncoder, ensure_ascii=False)

        return jsonData

    @staticmethod
    def viewUser_userRecharge_payment_list_to_json(dataList):
        data = []
        for row in dataList:
            result = {}
            result['userName'] = row[0]
            result['userPhone'] = row[1]
            result['userEmail'] = row[2]
            result['userNickName'] = row[3]
            result['userStatus'] = row[4]

            result['userRegIp'] = row[5]
            result['userRegTime'] = row[6]
            result['userAvatar'] = row[7]
            result['memberId'] = row[8]
            result['userId'] = row[9]

            result['invited_code'] = row[10]
            result['member_code'] = row[11]
            result['member_name'] = row[12]
            result['member_email'] = row[13]
            result['member_points'] = row[14]


            result['member_balance'] = row[15]
            result['member_expired'] = row[16]
            result['industry_id'] = row[17]  ######
            result['province_id'] = row[18]
            result['city_id'] = row[19]


            result['district_id'] = row[20]
            result['member_favorite_count'] = row[21]
            result['member_address'] = row[22]
            result['member_busness'] = row[23]
            result['member_type'] = row[24]

            result['sp_knowledge'] = row[25]
            result['sp_revenue'] = row[26]
            result['sp_employee'] = row[27]
            result['sp_industrycode'] = row[28]
            result['tm_industrycode'] = row[29]


            result['tm_deptcode'] = row[30]
            result['tm_level'] = row[31]
            result['id'] = row[32]
            result['rechargeNumber'] = row[33]
            result['amount'] = row[34]

            result['status'] = row[35]
            result['creatTime'] = row[36]
            result['completeTime'] = row[37]
            result['paymentId'] = row[38]
            result['paymentName'] = row[39]

            result['paymentImgUrl'] = row[40]
            result['paymentRemark'] = row[41]
            result['paymentType'] = row[42]
            result['paymentPoundageType'] = row[43]
            result['paymentPoundageAmount'] = row[44]

            result['paymentSort'] = row[45]
            result['paymentIsLock'] = row[46]
            result['paymentApiPath'] = row[47]
            data.append(result)
        jsonData = json.dumps(data, cls=MyJsonEncoder, ensure_ascii=False)
        return jsonData

    @staticmethod
    def viewUser_pointLog_list_to_json(dataList):
        data = []
        for row in dataList:
            result = {}
            result['userName'] = row[0]
            result['userPhone'] = row[1]
            result['userEmail'] = row[2]
            result['userNickName'] = row[3]
            result['userStatus'] = row[4]

            result['userRegIp'] = row[5]
            result['userRegTime'] = row[6]
            result['userAvatar'] = row[7]
            result['memberId'] = row[8]
            result['userId'] = row[9]

            result['invited_code'] = row[10]
            result['member_code'] = row[11]
            result['member_name'] = row[12]
            result['member_email'] = row[13]
            result['member_points'] = row[14]

            result['member_balance'] = row[15]
            result['member_expired'] = row[16]
            result['industry_id'] = row[17]  ######
            result['province_id'] = row[18]
            result['city_id'] = row[19]

            result['district_id'] = row[20]
            result['member_favorite_count'] = row[21]
            result['member_address'] = row[22]
            result['member_busness'] = row[23]
            result['member_type'] = row[24]

            result['sp_knowledge'] = row[25]
            result['sp_revenue'] = row[26]
            result['sp_employee'] = row[27]
            result['sp_industrycode'] = row[28]
            result['tm_industrycode'] = row[29]

            result['tm_deptcode'] = row[30]
            result['tm_level'] = row[31]

            result['id'] = row[32]
            result['point_item_name'] = row[33]
            result['point_value'] = row[34]
            result['remark'] = row[35]

            result['is_income'] = row[36]
            result['create_time'] = row[37]

            data.append(result)
        jsonData = json.dumps(data, cls=MyJsonEncoder, ensure_ascii=False)
        return jsonData


    """
    view_user
    """
    @staticmethod
    def viewUser_list_to_json(dataList):
        data = []
        for row in dataList:
            result = {}
            result['userName'] = row[0]
            result['userPhone'] = row[1]
            result['userEmail'] = row[2]
            result['userNickName'] = row[3]
            result['userStatus'] = row[4]

            result['userRegIp'] = row[5]
            result['userRegTime'] = row[6]
            result['userAvatar'] = row[7]
            result['memberId'] = row[8]
            result['userId'] = row[9]

            result['invitedCode'] = row[10]
            result['memberCode'] = row[11]
            result['memberName'] = row[12]
            result['memberEmail'] = row[13]
            result['memberPoints'] = row[14]

            result['memberBalance'] = row[15]
            result['memberExpired'] = row[16]
            result['industryId'] = row[17]  ######
            result['provinceId'] = row[18]
            result['cityId'] = row[19]

            result['districtId'] = row[20]
            result['memberFavoriteCount'] = row[21]
            result['memberAddress'] = row[22]
            result['memberBusness'] = row[23]
            result['memberType'] = row[24]

            result['spKnowledge'] = row[25]
            result['spRevenue'] = row[26]
            result['spEmployee'] = row[27]
            result['spIndustrycode'] = row[28]
            result['tmIndustrycode'] = row[29]

            result['tmDeptcode'] = row[30]
            result['tmLevel'] = row[31]
            #register_code  is_vip  LYM添加
            result['registerCode'] = row[32]
            result['isVip'] = row[33]
            data.append(result)

        jsonData = json.dumps(data, cls=MyJsonEncoder, ensure_ascii=False)
        return jsonData